#pragma once

#ifndef CVGABOR
#define CVGABOR

#include "opencv2/myopencv.hpp"

#define GABOR_DISCRETE_LEVEL (1<<15)
#define MATHPI 3.14159265358979324

const double UH = 1.69f;
const double UL = 0.19f;
const int NSIZE = 5;

class CGaborFilter;

CvArr* cvConv(const CvArr* src, const CvMat* kernel);
void cvGaborConv(const CvArr* src, CvMat* dst1, const CGaborFilter* filter);

class CGaborFilter {
protected:
	// Parameters used to build filter bank
	int m_ScaleTotal, m_OrientationTotal;	//The total number of scales and orientations used to partition the spectrum. 

	CvMat* m_GaborFilterRe;		// Real part of the current Gabor filter
	CvMat* m_GaborFilterIm;	    //Imaginary part of the current Gabor filter

	void AllocateMat()
	{
		ReleaseMat();
		int nWindowSize=2*NSIZE+1;
		m_GaborFilterIm = cvCreateMat(nWindowSize, nWindowSize,CV_32F);
		m_GaborFilterRe = cvCreateMat(nWindowSize, nWindowSize,CV_32F);
	}

	void ReleaseMat()
	{
		if (m_GaborFilterRe)
		{
			cvReleaseMat(&m_GaborFilterRe);
		}
		if (m_GaborFilterIm)
		{
			cvReleaseMat(&m_GaborFilterIm);
		}
	}
	CGaborFilter(void):m_GaborFilterRe(0),m_GaborFilterIm(0)
	{
		AllocateMat();
	}
public:

	~CGaborFilter()
	{
		ReleaseMat();
	}
	CvMat* GetFilterMatRe() const {return m_GaborFilterRe;}
	CvMat* GetFilterMatIm() const {return m_GaborFilterIm;}
	static CGaborFilter* GenerateGaborFilter( int nScale, int nOrientation, int m_ScaleTotal = 5, int m_OrientationTotal = 8);

};

#endif